package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.dao.MallDeliveryAddressDao;
import com.sc.nft.entity.MallDeliveryAddress;
import com.sc.nft.entity.dto.AddAddressDTO;
import com.sc.nft.entity.dto.ModificationAddRessDTO;
import com.sc.nft.entity.vo.MallDeliveryAddressVO;
import com.sc.nft.service.MallDeliveryAddressService;
import com.sc.nft.sup.PageRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收货地址表(MallDeliveryAddress)表服务实现类
 *
 * @author wwc
 * @since 2023-05-11 16:35:35
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class MallDeliveryAddressServiceImpl implements MallDeliveryAddressService {

    private final MallDeliveryAddressDao mallDeliveryAddressDao;

    @Override
    public MallDeliveryAddress getAddressByUserIdAndId(Long userId, Long id) {
        return mallDeliveryAddressDao.getAddressByUserIdAndId(userId,id);
    }
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addAddress(AddAddressDTO addAddressDTO, Long userId) {
        // 如果当前新增条目是设置为默认地址了,那么需要将其他的地址设置为非默认地址
        disableDefaultShippingAddress(addAddressDTO.getDefaultAddress(), userId);
        // 新增地址
        MallDeliveryAddress mallDeliveryAddress = new MallDeliveryAddress();
        BeanUtil.copyProperties(addAddressDTO, mallDeliveryAddress);
        mallDeliveryAddress.setUserId(userId);
        mallDeliveryAddress.insert();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void modificationAddress(ModificationAddRessDTO modificationAddRessDTO, Long userId) {
        // 如果当前修改条目是设置为默认地址了,那么需要将其他的地址设置为非默认地址
        disableDefaultShippingAddress(modificationAddRessDTO.getDefaultAddress(), userId);
        // 修改地址
        MallDeliveryAddress mallDeliveryAddress = new MallDeliveryAddress();
        BeanUtil.copyProperties(modificationAddRessDTO, mallDeliveryAddress);
        mallDeliveryAddress.updateById();
    }

    @Override
    public MallDeliveryAddressVO getDataById(Long id) {
        MallDeliveryAddress mallDeliveryAddress = mallDeliveryAddressDao.getById(id);
        MallDeliveryAddressVO mallDeliveryAddressVO = new MallDeliveryAddressVO();
        BeanUtil.copyProperties(mallDeliveryAddress,mallDeliveryAddressVO);
        return mallDeliveryAddressVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteById(Long id) {
        mallDeliveryAddressDao.removeById(id);
    }

    @Override
    public Page<MallDeliveryAddressVO> page(PageRequest pageRequest,Long userId) {
        Page<MallDeliveryAddressVO> pageData = mallDeliveryAddressDao.getPageData(pageRequest,userId);
        return pageData;
    }

    /**
     * 获取默认收货地址
     *
     * @param userId
     * @return
     */
    @Override
    public MallDeliveryAddressVO getDefaultAddress(Long userId) {
        return mallDeliveryAddressDao.getDefaultAddress(userId);
    }

    /**
     * 关闭默认收货地址
     *
     * @param defaultAddress
     * @param userId
     */
    private void disableDefaultShippingAddress(Boolean defaultAddress, Long userId) {
        if (defaultAddress) {
            mallDeliveryAddressDao.updateDefaultAddress(userId);
        }
    }

}
